﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TWS.Cart.Business
{
    /// <summary>
    /// Summary description for ShoppingCartManager
    /// </summary>
    public class ShoppingCartManager
    {
        public ShoppingCartManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private List<CartItem> _lstItems = new List<CartItem>();
        public List<CartItem> LstItems
        {
            get
            {
                if (HttpContext.Current.Session["Cart"] != null)
                    _lstItems = (List<CartItem>)HttpContext.Current.Session["Cart"];
                else
                    HttpContext.Current.Session["Cart"] = _lstItems;

                return _lstItems;
            }
            set
            {
                _lstItems = value;
                HttpContext.Current.Session["Cart"] = _lstItems;
            }
        }

		public ShippingItem GetShippingQuote()
		{
			//Some formula to calcultate shipping based in dimension and wheight
			ShippingItem si = new ShippingItem(1,"Estafeta",245);
			return si;
		}

		/// <summary>
		/// The total of the items. (Doesnt include the Shipping or taxes)
		/// </summary>
		/// <returns></returns>
		public decimal GetOrderTotal()
		{
			decimal res = 0;

			foreach (var cartItem in LstItems)
			{
				res += cartItem.SubTotal;
			}

			return res;
		}

		/// <summary>
		/// The total including shipping.
		/// </summary>
		/// <returns></returns>
		public decimal GetOrderSubTotal()
		{
			decimal res = GetOrderTotal()+GetShippingQuote().Cost;
			
			return res;
		}


		/// <summary>
		/// The total of items in cart.
		/// </summary>
		/// <returns></returns>
		public int GetTotalItems()
		{
			return LstItems.Count();
		}
    }

	public class ShippingItem
	{
		public  ShippingItem()
		{
		}

		public ShippingItem(long companyId, string company, decimal cost)
		{
			Company = company;
			CompanyId = companyId;
			Cost = cost;
		}

		public string Company { get; set; }
		public long CompanyId { get; set; }
		public decimal Cost { get; set; }
	}
}
